package com.cloud.wms.ui.inside.revert;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.RevertContact;
import com.cloud.wms.ui.inside.entity.RevertEntity;
import com.cloud.wms.ui.inside.presenter.RevertPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.PosEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-17 15:52
 * @Description:
 */
public class RevertPosActivity extends BaseBarListActivity<PosEntity, RevertPresenter> implements RevertContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_commodity_code)
    TextView tvCommodityCode;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    private CommodityEntity commodityEntity;
    private boolean isCancelOrder;

    @Override
    protected String barTitle() {
        return "还货上架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_revert_upshelf;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_revert_pos;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        isCancelOrder = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, true);
        commodityEntity = getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        tvCommodityCode.setText(commodityEntity.getBarCode());
        tvCommodityName.setText(commodityEntity.getCommodityName());
        mPresenter.loadCommoditySortPos(commodityEntity.getCommodityId(), isCancelOrder ? 1 : 2);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);

        commodityEntity.setPosCode(result);
        if (isCancelOrder) {
            mPresenter.upShelfPosAmount(commodityEntity.getCommodityId(), result);
        } else {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                PosEntity posEntity = mAdapter.getData().get(i);
                if (result.equalsIgnoreCase(posEntity.getPosCode())) {
                    RevertInventoryActivity.start(this, commodityEntity, isCancelOrder ? 1 : 2);
                }
            }
        }
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, PosEntity item) {
        helper.setText(R.id.tv_pos_code, item.getPosCode())
                .setText(R.id.tv_pos_amount, item.getAmount() + "");
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showEmpty() {
        mAdapter.setEmptyView(getEmptyDataView());
    }

    @Override
    public void showError() {
        mAdapter.setEmptyView(getErrorView());
    }

    protected View getEmptyDataView() {
        View notDataView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_empty, rvList, false);
        notDataView.setOnClickListener(v -> {
            mPresenter.loadCommoditySortPos(commodityEntity.getCommodityId(), isCancelOrder ? 1 : 2);
        });
        return notDataView;
    }

    protected View getErrorView() {
        View errorView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_error, rvList, false);
        errorView.setOnClickListener(v -> {
            mPresenter.loadCommoditySortPos(commodityEntity.getCommodityId(), isCancelOrder ? 1 : 2);
        });
        return errorView;
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case RevertPresenter.GET_REVERT_POS:
                mAdapter.setNewInstance(((ArrayList<PosEntity>) message.obj));
                break;
            case RevertPresenter.GET_REVERT_AMOUNT:
                RevertEntity revertEntity = (RevertEntity) message.obj;
                commodityEntity.setPosID(revertEntity.getBatchList().get(0).getPosID());
                RevertBatchActivity.start(this, commodityEntity, ((RevertEntity) message.obj), isCancelOrder ? 1 : 2);
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                this.finish();
                break;
        }
    }

    public static void start(Activity activity, CommodityEntity commodityBean, boolean isCancel) {
        Intent intent = new Intent(activity, RevertPosActivity.class);
        intent.putExtra(IntentKey.INFO_KEY, commodityBean);
        intent.putExtra(IntentKey.TYPE_KEY, isCancel);
        activity.startActivity(intent);
    }
}
